import {otherRouter, appRouter} from '../../router/router';
import Util from '../../libs/util';

const app = {
    state: {
        menuList: [],
        currentPath: [
            {
                title: '首页',
                path: '/home',
                name: 'home_index'
            }
        ], // 面包屑数组
        routers: [
            otherRouter,
            ...appRouter
        ]
    },
    mutations: {
        updateMenulist (state, permissions) {
            let menuList = [];
            appRouter.forEach((item, index) => {
                if (item.permission !== undefined) {
                    if (Util.showThisRoute(item.permission, permissions)) {
                        if (item.children && item.children.length > 0) {
                            var childrenArr = [];
                            childrenArr = item.children.filter(child => {
                                if (child.permission !== undefined) {
                                    if (Util.showThisRoute(child.permission, permissions)) {
                                        return child;
                                    }
                                } else {
                                    return child;
                                }
                            });
                            if(childrenArr.length > 0) {
                                item.children = childrenArr;
                                menuList.push(item);
                            }
                        }
                    }
                }else {
                    if (item.children && item.children.length > 0) {
                        var childrenArr = [];
                        childrenArr = item.children.filter(child => {
                            if (child.permission !== undefined) {
                                if (Util.showThisRoute(child.permission, permissions)) {
                                    return child;
                                }
                            } else {
                                return child;
                            }
                        });
                        if(childrenArr.length > 0) {
                            item.children = childrenArr;
                            menuList.push(item);
                        }
                    }
                }
            });
            state.menuList = menuList;
        },
        setCurrentPath (state, pathArr) {
            state.currentPath = pathArr;
        },
    }
};

export default app;
